/*
 * @Description:
 * @Author: mxy
 * @Date: 2022-12-10 20:31:16
 * @LastEditors: mxy
 * @LastEditTime: 2022-12-10 21:41:34
 */
import axios from 'axios'
export default {
  namespaced: true,
  state () {
    return {
      list: []
    }
  },
  mutations: {
    initList (state, list) {
      state.list = list
    },
    updateMount (state, obj) {
      const res = state.list.find(item => item.id === obj.id)
      if (res.goods_count === 1 && obj.symbol === -1) return
      res.goods_count += obj.symbol
    },
    updateState (state, id) {
      const res = state.list.find(item => item.id === id)
      res.goods_state = !res.goods_state
    },
    updateAllState (state, boo) {
      state.list.forEach(item => {
        item.goods_state = boo
      })
    }
  },
  actions: {
    async getList (store) {
      const { data: res } = await axios.get('http://www.itcbc.com:3006/api/cart')
      console.log(res)
      store.commit('initList', res.list)
    }

  },
  getters: {
    all (state) {
      return state.list.every(item => item.goods_state)
    },
    total (state) {
      const arr = state.list.filter(item => item.goods_state)
      return arr.reduce((t, c) => t + c.goods_count * c.goods_price, 0)
    },
    amount (state) {
      const arr = state.list.filter(item => item.goods_state)
      return arr.reduce((t, c) => t + c.goods_count, 0)
    }
  }
}
